import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import '../commons.dart';

/**
 * get请求和动态修改文字
 */
class GetRequestPage extends StatefulWidget {
  const GetRequestPage({Key? key}) : super(key: key);

  @override
  _GetRequestPageState createState() => _GetRequestPageState();
}

class _GetRequestPageState extends State<GetRequestPage> {
  TextEditingController typeController = TextEditingController();

  String showtext = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('天上人间'),
      ),
      body: Column(
        children: [
          TextField(
            controller: typeController,
            decoration: const InputDecoration(
                contentPadding: EdgeInsets.all(10.0),
                labelText: '美女类型',
                helperText: '请输入你喜欢的类型'),
            autofocus: false,
          ),
          ElevatedButton(
            onPressed: _choiceA,
            child: Text(
              '选择完毕',
              overflow: TextOverflow.ellipsis,
              maxLines: 2,
            ),
          ),
          Text(showtext)
        ],
      ),
    );
  }

  void _choiceA() {
    print('开始选择你喜欢的类型.....');

    if (typeController.text.toString().trim().isEmpty) {
      showDialog(
          context: context,
          builder: (context) => const AlertDialog(
                title: Text('美女类型不能为空'),
              ));
    } else {
      getHttp(typeController.text.toString()).then((vale) {
        setState(() {
          showtext = vale['data'];
        });
      });
    }
  }

  Future getHttp(String texttype) async {
    try {
      Response response;
      response = await Dio().get(Commons.home);
      return response.data;
    } catch (e) {
      return print(e);
    }
  }
}
